import os, sys, time, yaml
from ivic.core import Logging
from ivic.core import Utils

logger = Logging.get_logger('ivic.webfarm.WebFarmUtils') 

def get_vm_uuid(vm_yaml):
    try:
        vm = yaml.load(file(vm_yaml))
        logger.debug(vm)
        return vm['uuid']
    except Exception, e:
        logger.error("%s:%s" % (sys._getframe().f_code.co_name, e))
        if os.path.isfile(vm_yaml):
            try:
                vmfile = open(vm_yaml, 'r')
                vm = vmfile.readline()
                if vm[-1] == '\n': vm = vm[:-1]
                vm_uuid = vm.split(':')[1].strip()
                return vm_uuid
            except Exception, e:
                logger.error("%s:%s" % (sys._getframe().f_code.co_name, e))
    return "not-set"

def get_webfarm_vip():
    try:
        ipaddr = Utils.external_address()
        # determine the corresponding virtual ip address.
        if ipaddr.startswith('10'):
            #for we know: '10.***.***.100' 
            return ipaddr[:-3] + "250"
        else: return ''
    except Exception, e:
        raise
        
